﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using ProtoBuf;


namespace DataSource
{
    public class LoadMarketData: ILoadData
    {
        private string lv2Path;
        private string tickPath;
        private string date;
        public LoadMarketData(string date)
        {
            this.date = date;
            lv2Path = @"D:\yy_temp\MarketDataForTzero\Level2\"+ date + ".level2.bin";
            tickPath = @"D:\yy_temp\MarketDataForTzero\Tick\" + date + ".tick.bin";
        }

        public List<Level2Struct> LoadMarketLv2(List<string> stkList)
        {
            //LoadCsvFromHS lc = new LoadCsvFromHS(date);
            LoadCsvFromDFW lc = new LoadCsvFromDFW(date);
            if(stkList.Count < 4000) //如果只是部分股票,读原始文件
            {
                return lc.LoadMarketLv2(stkList);
            }

            if (File.Exists(lv2Path))
            {
                //反序列化
                using(Stream sr = File.OpenRead(lv2Path))
                {
                    List<Level2Struct> lls = Serializer.Deserialize<List<Level2Struct>>(sr);
                    return lls;
                }
            }
            else
            {         
                List<Level2Struct> lls = lc.LoadMarketLv2(stkList);
                SaveMarketLv2Data(lls); //保存下
                return lls;
            }
        }


        public void SaveMarketLv2Data(List<Level2Struct> lls)
        {
            //序列化
            using (Stream sr = File.Create(lv2Path))
            {
                Serializer.Serialize(sr, lls);
            }
        }

        public List<TickStruct> LoadMarketTick(List<string> stkList)
        {
            return new List<TickStruct>();
        }



        public List<Level2Struct> LoadLv2(string code)
        {
            LoadCsvFromHS lc = new LoadCsvFromHS(date);
            return lc.LoadLv2(code);
        }

        public List<TickStruct> LoadTick(string code)
        {
            LoadCsvFromHS lc = new LoadCsvFromHS(date);
            return lc.LoadTick(code);
        }
    }
}
